package com.softhis.rekrutacja.client.presenter;

import com.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.softhis.rekrutacja.client.MainEventBus;
import com.softhis.rekrutacja.client.commons.ClientSession;
import com.softhis.rekrutacja.client.view.LoginFormView;
import com.softhis.rekrutacja.client.view.widgets.interfaces.IButton;
import com.softhis.rekrutacja.client.view.widgets.interfaces.ITextField;
import com.softhis.rekrutacja.shared.beans.LoginBean;
import com.softhis.rekrutacja.shared.services.UserServiceAsync;

/**
 * @author Sasak
 * 
 */
@Presenter(view = LoginFormView.class)
public class LoginFormPresenter extends BasePresenter<LoginFormPresenter.LoginFormViewInterface, MainEventBus> {

	@Inject
	private UserServiceAsync userService = null;

	/**
	 * Create a remote user service.
	 */
	public void setUserService(UserServiceAsync userService) {
		this.userService = userService;
	}

	@Override
	public void bind() {

		view.getLoginButton().addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				logIn();
			}
		});
	}

	/**
	 * clear and sets view in main center section
	 */
	public void onSetLogin() {
		view.clearForm();
		eventBus.setContent((Widget) view);
	}

	protected void logIn() {

//		eventBus.maskCenterSection();
		
		String login = view.getLoginField().getText();
		String password = view.getPasswordField().getText();

		userService.loginUser(login, password, new AsyncCallback<LoginBean>() {

			public void onFailure(Throwable caught) {
				Log.error("[LOGIN FORM PRESENTER] could not log in", caught);
				MessageBox.alert("Komunikat", "Utracono połącznie z serwerem, spróbuj ponownie później", null);
			}

			public void onSuccess(LoginBean result) {
//				if (result.getSessionId() == null) {
				if(result.getLoggedUser() == null){
					Log.error("[LOGIN FORM PRESENTER] username or password incorrect");
					view.setWarning("Podano nieprawidłowe dane");
					MessageBox.alert("Logowanie", "Wprowadź poprawne dane", null);
					
				} else {
					Log.debug("[LOGIN FORM PRESENTER] user logged in, session_id=" + result.getSessionId());
					ClientSession.setSessionId(result.getSessionId());
					ClientSession.setLoggedUser(result.getLoggedUser());
					view.clearForm();
					eventBus.setLogged();
				}
			}
		});
	}

	public interface LoginFormViewInterface {

		public ITextField getLoginField();

		public ITextField getPasswordField();

		public IButton getLoginButton();

		// public IButton getNewUserButton();
		//
		// public IButton getPasswordRemindButton();

		/**
		 * Clears login form
		 */
		public void clearForm();

		/**
		 * Checks whether login panel is filled correctly
		 * 
		 * @return
		 */
		public boolean isFormValid();

		/**
		 * Sets message on the center section message label
		 * 
		 * @param message
		 */
		public void setMessage(String message);

		/**
		 * Sets red warning on the center section message label
		 * 
		 * @param message
		 */
		public void setWarning(String warning);
	}

}
